// pages/shoppingCart/shoppingCart.js
import Http from '../../utils/http.js';
let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    checkedAll: false,//全选
    noSelect: true,//是否有勾选项
    editable: false,
    delBtnWidth: 90,
    totalPrice: 0,//总价
    loading: false,
    shoppingCartList: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // this.updateCheckedAll();
    this.computedPrice();
  },
  //获取购物车列表
  getShoppingCartList() {
    this.setData({
      loading: true
    })
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    Http.Request('GET', '/api/customer/shoppingCart/getCartList/v1').then(res => {
      if (res && res.length) {
        res.forEach(o => {
          o.selected = false
        })
      }
      this.setData({
        shoppingCartList: res || [],
        loading: false
      })
      wx.hideLoading();
      this.updateCheckedAll();
      this.computedPrice();
    }).catch(err => {
      wx.hideLoading();
      app.globalFn.reLoad(this.getShoppingCartList, err);

    })
  },
  //结算
  jsClick() {
    if (this.data.noSelect)return
    const commodityList = this.data.shoppingCartList.filter(o=>{
      return o.selected
    })
    wx.navigateTo({
      url: `../shiwuList/shiwuDetail/createOrder/index?commodityList=${JSON.stringify(commodityList)}`
    })
  },
  goShoppingClick() {
    wx.switchTab({
      url: '/pages/home/home',
    });
  },
  //全选
  checkedAllClick() {
    let list = this.data.shoppingCartList;
    for (let i = 0; i < list.length; i++) {
      list[i].selected = !this.data.checkedAll
    }
    this.setData({
      shoppingCartList: list,
      checkedAll: !this.data.checkedAll,
      noSelect: this.data.checkedAll
    })
    this.computedPrice();
  },
  //更新购物车数据
  updateBblist(e) {
    this.setData({
      shoppingCartList: e.detail
    })
    this.updateCheckedAll();
    this.computedPrice()
  },
  //计算总价
  computedPrice() {
    const list = this.data.shoppingCartList;
    let sum = 0;
    for (let i = 0; i < list.length; i++) {
      if (list[i].selected) {
        sum += (Number(list[i].count) * Number(list[i].commodityPrice))
      }
    }
    this.setData({
      totalPrice: sum
    })
  },
  //更新全选状态
  updateCheckedAll() {
    //非全选
    const notCheckedAll = this.data.shoppingCartList.some(item => {
      return !item.selected
    })
    //选中至少一个
    const select = this.data.shoppingCartList.some(item => {
      return item.selected
    })
    this.setData({
      checkedAll: !notCheckedAll,
      noSelect: !select
    })
  },
  //编辑
  editClick() {
    let shoppingCartList = this.data.shoppingCartList;
    const data = shoppingCartList.map(o => {
      return { commodityCode: o.commodityCode, count: o.count, productBizid: o.productBizid }
    })
    //点击完成
    if (this.data.editable) {
      wx.showLoading({
        title: '加载中',
        mask: true
      })
      Http.Request('PUT', `/api/customer/shoppingCart/update/v1`, data).then(res => {
        console.log(res);
        this.setData({
          editable: !this.data.editable,
        })
        for (let i = 0; i < shoppingCartList.length; i++) {
          shoppingCartList[i].delShow = this.data.editable ? `z-index:5` : `z-index:4`
        }
        this.setData({
          shoppingCartList: shoppingCartList
        })
        wx.hideLoading();
      }).catch(err => {
        wx.showToast({
          title: err,
          icon: "none",
          duration: app.globalData.duration
        })
        wx.hideLoading();
      })
    }
    if (!this.data.editable) {
      this.setData({
        editable: !this.data.editable,
      })
      for (let i = 0; i < shoppingCartList.length; i++) {
        shoppingCartList[i].delShow = this.data.editable ? `z-index:5` : `z-index:4`
      }
      this.setData({
        shoppingCartList: shoppingCartList
      })
    }

  },
  //获取实际删除按钮的宽度
  getDelBtnWidth(w) {
    if (w && w.detail) {
      this.setData({
        delBtnWidth: w.detail
      })
    }

  },
  //批量删除
  handleDelBatch(){
    let shoppingCartList = this.data.shoppingCartList
    const bizidList = shoppingCartList.filter(o=>{
      return o.selected
    }).map(s=>{
      return s.bizid
    })
    if (bizidList.length){
      wx.showLoading({
        title: '删除中',
        mask: true
      })
      Http.Request('DELETE', '/api/customer/shoppingCart/batchDelete/v1', { bizid: bizidList}).then(res=>{
        wx.hideLoading();
        wx.showToast({
          title: '删除成功',
          icon: 'success',
          duration: app.globalData.duration
        })
        shoppingCartList = shoppingCartList.filter(o=>{
          return !o.selected
        })
        this.setData({
          shoppingCartList
        })
      }).catch(err => {
        wx.hideLoading();
        wx.showToast({
          title: err,
          icon: 'none',
          duration: app.globalData.duration
        })
      })
    }
    
  },
  onShow(){
    this.getShoppingCartList()
  },
  onShareAppMessage() {
    app.globalFn.onShareAppMessage()
  }
})